/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jxta;

import atributos.ManagerAtributos;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.jxta.impl.id.CBID.PeerID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.socket.JxtaSocket;
import utilidades.Manejador;
import utilidades.ManejadorConexion;

/**
 *
 * @author Eduardo
 */
public class SocketClient {

    private final PeerID PEER_ID = null;
    private final int TIME_OUT = 30000;
    private final boolean RELIABLE = true;
    private final String id;
    private final PipeAdvertisement pipe;
    private final PeerGroup netPeerGroup;
    private final String manejadorStr;
    private final String ruta;
    private int COMPORTAMIENTO;
    private  byte[] llaveSecretaCifrada;

    SocketClient(String id, PeerGroup peerGroup, PipeAdvertisement pipe, String ruta, byte[]  llaveSecretaCifrada) {
        this.id = id;
        this.pipe = pipe;
        this.netPeerGroup = peerGroup;
        this.ruta = ruta;
        this.COMPORTAMIENTO = Manejador.RECIBIR;
        this.manejadorStr =ManejadorConexion.class.getName();
        this.llaveSecretaCifrada = llaveSecretaCifrada;
    }

    SocketClient(String id,
            PeerGroup peerGroup,
            PipeAdvertisement pipe,
            String ruta,
            int comportamiento,
            String manejador,
            byte[] llaveSecretaCifrada){
        this.netPeerGroup = peerGroup;
        this.pipe = pipe;
        this.ruta = ruta;
        this.id = id;
        this.COMPORTAMIENTO = comportamiento;
        this.manejadorStr = manejador;
        this.llaveSecretaCifrada = llaveSecretaCifrada;
    }



    public void empezar() {
        try {

            long start = System.currentTimeMillis();
            System.out.println("Connecting to the server");
            JxtaSocket socket = new JxtaSocket(netPeerGroup,
                    // no specific peerid
                    PEER_ID,
                    pipe,
                    // connection timeout: 5 seconds
                    TIME_OUT,
                    // reliable connection
                    RELIABLE);
            Manejador manejador = (Manejador) Class.forName(manejadorStr).newInstance();
            //1. Asignar el socket.
            manejador.setSocket(socket);
            //2. Asignar el comportamiento.
            manejador.setComportamiento(COMPORTAMIENTO);
            if (COMPORTAMIENTO == Manejador.RECIBIR) {
                //2.1. Asignar el archivo a descargar. Si aplica
                manejador.setIdArchivo(id);
            }
            manejador.setLlaveSecreta(llaveSecretaCifrada);
            //3. Asignar la ruta.
            manejador.setPathname(ruta);
            Thread thread = new Thread(manejador, "Connection Handler Thread");
            thread.start();

        } catch (IOException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SocketClient.class.getName()).log(Level.SEVERE, null, ex);

        }
    }
}
